/**
  **************************************************************************
  * @file     at32f403a_407_board.c
  * @version  v2.1.2
  * @date     2022-08-16
  * @brief    set of firmware functions to manage leds and push-button.
  *           initialize delay function.
  **************************************************************************
  *                       Copyright notice & Disclaimer
  *
  * The software Board Support Package (BSP) that is made available to
  * download from Artery official website is the copyrighted work of Artery.
  * Artery authorizes customers to use, copy, and distribute the BSP
  * software and its related documentation for the purpose of design and
  * development in conjunction with Artery microcontrollers. Use of the
  * software is governed by this copyright notice and the following disclaimer.
  *
  * THIS SOFTWARE IS PROVIDED ON "AS IS" BASIS WITHOUT WARRANTIES,
  * GUARANTEES OR REPRESENTATIONS OF ANY KIND. ARTERY EXPRESSLY DISCLAIMS,
  * TO THE FULLEST EXTENT PERMITTED BY LAW, ALL EXPRESS, IMPLIED OR
  * STATUTORY OR OTHER WARRANTIES, GUARANTEES OR REPRESENTATIONS,
  * INCLUDING BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY,
  * FITNESS FOR A PARTICULAR PURPOSE, OR NON-INFRINGEMENT.
  *
  **************************************************************************
  */

#include "bsp_print.h"
#include <stdarg.h>


/**
  * @brief  retargets the c library printf function to the usart.
  * @param  none
  * @retval none
  */
int fputc(int ch, FILE *f)
{
  while(usart_flag_get(USART3, USART_TDBE_FLAG) == RESET);
  usart_data_transmit(USART3, ch);
  return ch;
}



int my_printf(const char *format, ...)
{
	uint8_t i=0;
    va_list arg;
    static char sendbuff[512] = {0};
    int lenght;

    va_start(arg, format);
    lenght = vsnprintf((char *)sendbuff, sizeof(sendbuff) + 1, (char *)format, arg);
    va_end(arg);

	for(i=0;i<lenght;i++){
		while(usart_flag_get(USART3, USART_TDBE_FLAG) == RESET);
		usart_data_transmit(USART3, sendbuff[i]);
	}

    return lenght;
}
/**
  * @brief  initialize uart
  * @param  baudrate: uart baudrate
  * @retval none
  */
void uart_print_init(uint32_t baudrate)
{
	gpio_init_type gpio_init_struct;
	/* enable the uart and gpio clock */
	crm_periph_clock_enable(CRM_USART3_PERIPH_CLOCK, TRUE);
	crm_periph_clock_enable(CRM_GPIOB_PERIPH_CLOCK, TRUE);

	gpio_default_para_init(&gpio_init_struct);

	/* configure the usart3 tx pin */
	gpio_init_struct.gpio_drive_strength = GPIO_DRIVE_STRENGTH_STRONGER;
	gpio_init_struct.gpio_out_type  = GPIO_OUTPUT_PUSH_PULL;
	gpio_init_struct.gpio_mode = GPIO_MODE_MUX;
	gpio_init_struct.gpio_pins = GPIO_PINS_10;
	gpio_init_struct.gpio_pull = GPIO_PULL_NONE;
	gpio_init(GPIOB, &gpio_init_struct);
	/* configure the usart3 rx pin */
	gpio_init_struct.gpio_drive_strength = GPIO_DRIVE_STRENGTH_STRONGER;
	gpio_init_struct.gpio_out_type  = GPIO_OUTPUT_PUSH_PULL;
	gpio_init_struct.gpio_mode = GPIO_MODE_INPUT;
	gpio_init_struct.gpio_pins = GPIO_PINS_11;
	gpio_init_struct.gpio_pull = GPIO_PULL_UP;
	gpio_init(GPIOB, &gpio_init_struct);

	/* configure uart param */
	usart_init(USART3, baudrate, USART_DATA_8BITS, USART_STOP_1_BIT);
	usart_transmitter_enable(USART3, TRUE);
	usart_enable(USART3, TRUE);
}

